function [ binned_power times subject channel condition mergedFrequencies ] = TFC1a_LoadDataAndCalculateBins( timefrequencyFilename, frequencyBins )
%TFC1A_LOADDATAANDCALCULATEMEAN Summary of this function goes here
%   Detailed explanation goes here

[ subject, channel, channelType, condition, frequencies, times ] = GetTimeFrequencyRowByFilename(timefrequencyFilename);
SOURCE = load(timefrequencyFilename);

% nrOfEpochs is currently not in database..
nrOfEpochs = size(SOURCE.power,3); %#ok<NASGU>

binned_power = zeros(length(frequencyBins), length(times), nrOfEpochs);
mergedFrequencies = cell(length(frequencyBins),1);
for i=1:length(frequencyBins)
    selectedFreqs = SOURCE.frequencies>=frequencyBins(i).min & SOURCE.frequencies<=frequencyBins(i).max;
    mergedFrequencies{i} = SOURCE.frequencies(selectedFreqs);
    binned_power(i,:,:) = mean(SOURCE.power(selectedFreqs,:,:), 1); 
end

end








%% OLD STUFF

function MergeFrequencyBins( frequencybins, sourcefile, targetfile )

% X = load(sourcefile);


subject = X.subject;
condition = X.condition;
channel = X.channel;
sources = X.sources;

% freqBands = {...
%      4     7   'Theta'      '/theta'  ; ...
%      7    13   'Alpha'      '/alpha'  ; ...
%     13    30   'Beta'       '/beta'   ; ...
%     30    70   'Low-Gamma'  'l/gamma' ; ...
%     70   130   'High-Gamma' 'h/gamma' ; ...
%     };
% 
% % for i = 1:size(frequencybins,1)
%     data.frequencybins(i).min = frequencybins(i,1);
%     data.frequencybins(i).max = frequencybins(i,2);
%     frbnd = find([freqBands{:,1}] >= frequencybins(i,1) & [freqBands{:,2}] <= frequencybins(i,2));
%     desc = '';
%     texdesc = '';
%     for j = 1:length(frbnd)
%         if j>1, desc = [desc '/']; texdesc = [texdesc '/']; end
%         desc = [desc freqBands{frbnd(j),3}];
%         texdesc = [texdesc freqBands{frbnd(j),4}];
%     end
%     data.frequencybins(i).description = [desc ' (' num2str(frequencybins(i,1)) '-' num2str(frequencybins(i,2)) 'Hz)'];
%     data.frequencybins(i).texdescription = [texdesc ' (' num2str(frequencybins(i,1)) '-' num2str(frequencybins(i,2)) 'Hz)'];
% end
% data.times = X.data.times;
data.epochs = X.data.epochs;

% data.powerAvg = zeros(length(data.frequencybins), length(data.times), length(data.epochs));
% for i=1:length(data.frequencybins)
%     %     tfcollapsed(i).frequencyrange = frequencies(i,:);
%     selectedFreqs = X.data.frequencies>=data.frequencybins(i).min & X.data.frequencies<=data.frequencybins(i).max;
%     data.frequencybins(i).mergedFrequencies = X.data.frequencies(selectedFreqs);
%     data.powerAvg(i,:,:) = mean(X.data.powerAvg(selectedFreqs,:,:), 1);
% %     data.powerStd(i,:,:) = ??
% end

disp(['Saving merged frequencies to "' targetfile '"...']);
save(targetfile, '-v7.3', 'subject', 'condition', 'channel', 'sources', 'data');
disp(['Merging frequencies in region "' channel '" (subject "' subject '", condition "' condition '") took ' num2str(toc(tStart)) 's']);

end